package com.mailbox.mailbox.controller;

import com.mailbox.mailbox.service.TypeService;
import com.mailbox.mailbox.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
/**
 *
 * @author
 */
@Api(tags = "问题类型接口")
@RequestMapping("/Admin")
public class TypeController {
    @Resource
    TypeService typeService;


    @PostMapping("/addType")
    @ApiOperation(value = "添加类型")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "typeName", value = "类型名字",required = true)
            }
    )
    public ResponseResult addType(String typeName){
        if(StringUtils.isAnyBlank(typeName)){
            return ResponseResult.error("问题类型为空,请重新填写");
        }
        return
                typeService.addType(typeName);
    }

    @PutMapping("/updateType")
    @ApiOperation(value = "修改类型名字")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "typeName", value = "类型名字",required = true),
                    @ApiImplicitParam(name = "typeId", value = "类型id",required = true)
            }
    )
    public ResponseResult updateType(String typeName,Integer typeId){
        if(StringUtils.isAnyBlank(typeName)){
            return ResponseResult.error("问题类型为空,请重新填写");
        }
        if(typeId==null){
            return ResponseResult.error("类型id为空");
        }
        return typeService.updateType(typeId, typeName);
    }

    @DeleteMapping("/deleteTypeById")
    @ApiOperation(value = "单个删除")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "typeId", value = "类型id",required = true)
            }
    )
    public ResponseResult deleteTypeById(Integer typeId){
        if(typeId==null){
            return ResponseResult.error("类型id为空");
        }
        return typeService.deleteTypeById(typeId);
    }

    @DeleteMapping("/batchDeleteType")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "typeId", value = "类型id", allowMultiple = true, dataTypeClass = List.class, paramType = "query")
    public ResponseResult batchDeleteType(@RequestParam(value = "typeId")List<Integer> typeId){
        return typeService.batchDeleteType(typeId);
    }
    @GetMapping("/findAllType")
    @ApiOperation(value = "后台遍历所有")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageSize", value = "每页条数",required = true),
                    @ApiImplicitParam(name = "pageNow", value = "当前页数",required = true)
            }
    )
    public ResponseResult findAllType(Integer pageSize,Integer pageNow){
        return typeService.findAllType(pageSize, pageNow);
    }


    @GetMapping("/findTypeByNameLike")
    @ApiOperation(value = "根据关键字模糊查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageSize", value = "每页条数",required = true),
                    @ApiImplicitParam(name = "pageNow", value = "当前页数",required = true)
            }
    )
    public ResponseResult findTypeByNameLike(String keyWord,Integer pageNow,Integer pageSize){
        if(StringUtils.isAnyBlank(keyWord)){
            return ResponseResult.error("关键词为空");
        }
        return typeService.findTypeByNameLike(keyWord,pageNow, pageSize);
    }

}
